package cn.aloxc.custom;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.*;
import cn.aloxc.tianya.reader.R;
public class MyAdapter extends BaseAdapter{
	private ArrayList<Map<String, String>> values;
	private Activity activity = null;
	private int layout = 0 ;
	private LayoutInflater mInflater  = null;
	public MyAdapter(Activity activity,int layout,ArrayList<Map<String, String>> data){
		this.activity = activity;
		this.layout = layout;
		this.values = data;
		this.mInflater = activity.getLayoutInflater();
//		this.mInflater = LayoutInflater.from(this.activity.getApplicationContext());
	}
	public int getCount( ) {
		return values.size();
	}

	public Object getItem(int position) {
		return values.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int p = position;
		ViewHolder holder;
		//下面的代码效率没有使用convertView效果好
		//View v = activity.getLayoutInflater().inflate(this.layout,null);
		if(convertView == null){
			convertView = mInflater.inflate(this.layout, null);
			TextView name = (TextView)convertView.findViewById(R.id.test_list_simple_name);
			TextView sex = (TextView)convertView.findViewById(R.id.test_list_simple_sex);
			TextView mobile = (TextView)convertView.findViewById(R.id.test_list_simple_mobile);
			Button call = (Button)convertView.findViewById(R.id.test_list_simple_call);
			ImageView image = (ImageView)convertView.findViewById(R.id.test_list_simple_png);
			
			holder = new ViewHolder();
			holder.name = name;
			holder.sex = sex;
			holder.mobile = mobile;
			holder.icon = image;
			holder.call = call;
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		final String nvalue = values.get(position).get("name");
		final String svalue = values.get(position).get("sex");
		final String mvalue = values.get(position).get("mobile");
		final String ivalue = values.get(position).get("img");
		holder.name.setText(nvalue);
		holder.sex.setText(svalue);
		holder.mobile.setText(mvalue);
		holder.icon.setImageResource(Integer.parseInt(ivalue));
		holder.call.setText("呼叫");
		holder.call.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				activity.setTitle("n:"+nvalue+",s:"+svalue+",m:"+mvalue+",p:"+p);
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+mvalue));
				activity.startActivity(intent);
			}});
		convertView.setBackgroundResource(p%2 == 0?R.color.myAdpter_grid:R.color.myAdpter_white);
		return convertView;
	}
	private class ViewHolder{
		TextView name;
		TextView sex;
		TextView mobile;
		Button call;
		ImageView icon;
	}
}
